﻿using MQ.Core.Common;
using MQ.Core.Entity;
using MQ.Core.IRepository;
using MQ.Core.IService;
using MQ.Core.Model;
using System;
using System.Collections.Generic;
using System.Linq;

namespace MQ.Core.Service
{
    public class DictionaryService : IDictionaryService
    {
        private readonly IDictionaryRepository repository;
        public DictionaryService(IDictionaryRepository repository)
        {
            this.repository = repository;
        }
        #region  查询数据
        public IEnumerable<DictionaryEntity> GetList(string query)
        {
            return repository.GetList(query);
        }

        public IEnumerable<DictionaryEntity> GetPageList(Pagination pagination, string query)
        {
            return repository.GetPageList(pagination, query);
        }

        public IEnumerable<DictionaryTreeModel> GetTree(string query)
        {
            var dic = GetList(query);
            var data = new List<DictionaryTreeModel>();
            Func<long, List<DictionaryTreeModel>> func = null;
            func = new Func<long, List<DictionaryTreeModel>>(x =>
              {
                  var d = dic.Where(m => m.Id == x);
                  foreach(var i in d)
                  {
                      DictionaryTreeModel item = new DictionaryTreeModel() {
                          Id=i.Id,
                          ParentId=i.ParentId,
                          TypeId=i.TypeId,
                          Name=i.Name,
                          Value=i.Value,
                          Status=i.Status,
                          Remark=i.Remark
                      };
                      item.Children = func(i.Id);
                      data.Add(item);
                  }
                  return data;
              });
            return func(0);
        }
        #endregion

        #region  提交数据
        public bool Delete(long id)
        {
            return repository.Delete(id) > 0;
        }

        public bool Delete(string ids)
        {
            return repository.Delete(ids)>0;
        }

        public bool Delete(DictionaryEntity entity)
        {
            return repository.Delete(entity)>0;
        }
        public bool Save(DictionaryEntity entity)
        {
            if (entity.Id > 0)
                return repository.Update(entity)>0;
            return repository.Add(entity)>0;
        }
        #endregion
    }
}
